package com.forlgb.powercloud.authorization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forlgb.powercloud.authorization.entity.SysRole;
import com.forlgb.powercloud.authorization.entity.SysUser;
import com.forlgb.powercloud.authorization.mapper.SysRoleMapper;
import com.forlgb.powercloud.authorization.mapper.SysUserMapper;
import com.forlgb.powercloud.authorization.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyk
 * @since 2021-05-28
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService, UserDetailsService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        //查数据库
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("username", userName);
        SysUser sysUser = sysUserMapper.selectOne(qw);
        if (null != sysUser) {
            List<SysRole> roles = sysRoleMapper.getSysRolesByUserId(sysUser.getId());
            sysUser.setAuthorities(roles);
        }
        return sysUser;
    }
}
